package com.x.erp.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.SaleBill;
import com.x.erp.rpc.SaleBillRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;

/**
 * 销售单据
 */
@RestController
public class SaleBillController extends BaseController {

	@Reference
	private SaleBillRpcService saleBillRpcService;// 销售单据服务

	/**
	 * 查询销售单据信息
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getSaleBillList", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel getSaleBillList(SaleBill saleBill) {
		if (StringUtils.isEmpty(saleBill.getMarketId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "marketId不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", saleBillRpcService.selectSaleBillList(saleBill));
	}

	/**
	 * 查询销售单据总数
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getSaleBillNum", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel getSaleBillNum(SaleBill saleBill) {
		Map<String, Object> res = new HashMap<>();
		if (StringUtils.isEmpty(saleBill.getMarketId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "marketId不能为空！");
		}
		res.put("total", saleBillRpcService.selectSaleBillNum(saleBill));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询销售单据信息
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getSaleBillById", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel getSaleBillById(SaleBill saleBill) {
		if (StringUtils.isEmpty(saleBill.getBillId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "billId不能为空！");
		}
		if (StringUtils.isEmpty(saleBill.getMarketId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "marketId不能为空！");
		}
		List<SaleBill> saleBills = saleBillRpcService.selectSaleBillList(saleBill);
		if (saleBills != null && saleBills.size() > 0) {
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", saleBills.get(0));
		}
		return new ResultModel(ResultCode.RESULT_FAIL, "查询失败");
	}

	/**
	 * 新增单据信息
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/insertSaleBill", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel insertSaleBill(SaleBill saleBill) {
		if (saleBill == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "单据信息不能为空！");
		}
		if (saleBill.getBillDate() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "开票日期不能为空！");
		}
		if (!this.isNotNull(saleBill.getBillCode())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "发票代码不能为空！");
		}
		if (!this.isNotNull(saleBill.getBillNo())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "发票号码不能为空！");
		}
		if (saleBill.getBillPrice() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "发票金额不能为空！");
		} else {
			BigDecimal zero = BigDecimal.valueOf(0);
			if (saleBill.getBillPrice().compareTo(zero) <= 0) {
				return new ResultModel(ResultCode.RESULT_FAIL, "发票金额不能小于等于0！");
			}
		}
		if (!this.isNotNull(saleBill.getBillCarMark())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "厂牌不能为空！");
		}
		if (!this.isNotNull(saleBill.getBillCarVin())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车架号不能为空！");
		}
		// if(!this.isNotNull(saleBill.getBillCarEngineNo())) {
		// return new ResultModel(ResultCode.RESULT_FAIL, "发动机号不能为空！");
		// }
		if (!this.isNotNull(saleBill.getBillCarName())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆名称不能为空！");
		}
		if (!this.isNotNull(saleBill.getBillCarPlateHead()) || !this.isNotNull(saleBill.getBillCarPlateMiddle())
				|| !this.isNotNull(saleBill.getBillCarPlateCode())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆号牌不能为空！");
		}
		if (!this.isNotNull(saleBill.getCarTypeId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆类型不能为空！");
		}
		if (saleBill.getBrandId() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "品牌不能为空！");
		}
		if (saleBill.getSeriesId() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车系不能为空！");
		}
		if (saleBill.getModelId() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车型不能为空！");
		}
		if (!this.isNotNull(saleBill.getMarketId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "市场不能为空！");
		}
		if (!this.isNotNull(saleBill.getShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "店铺不能为空！");
		}
		if (!this.isNotNull(saleBill.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户信息不能为空！");
		}
		saleBill.setBillCreateTime(new Date());
		saleBillRpcService.insertSaleBill(saleBill);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功");
	}

	/**
	 * 修改单据信息
	 * 
	 * @param saleBill
	 * @return
	 */
	@RequestMapping(value = "/updateSaleBill", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel updateSaleBill(SaleBill saleBill) {
		if (saleBill == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "单据信息不能为空！");
		}
		if (!this.isNotNull(saleBill.getBillId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "单据ID不能为空！");
		}
		saleBillRpcService.updateSaleBill(saleBill);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功！");
	}

}
